package com.min.gps.web;




import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.min.gps.model.GPSInfo;
import com.min.gps.model.Message;
import com.min.gps.model.Response;
import com.min.gps.service.GPSService;
import com.min.gps.service.impl.GPSServiceImpl;
import com.min.gps.service.impl.WebSocketServiceImpl;
import com.min.gps.util.SpringContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.repository.query.Param;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.security.Principal;

/**
 * Created by yangyibo on 16/12/29.
 *
 */
@CrossOrigin
@Controller
public class WebSocketController {
    @Autowired
    private WebSocketServiceImpl ws;
    @Autowired
    private SimpMessagingTemplate messagingTemplate;

   // @Qualifier("GPSService")
    @Autowired
    private GPSServiceImpl dataService;

    @RequestMapping(value = "/ws")
    public String ws(){
        return  "ws";
    }
    @RequestMapping(value = "/ms")
    public String ms(){
        return  "ms";
    }
    @MessageMapping("/welcome")//浏览器发送请求通过@messageMapping 映射/welcome 这个地址。
    @SendTo("/topic/postMessage")//服务器端有消息时,会订阅@SendTo 中的路径的浏览器发送消息。
    public Response say(Message message) throws Exception {
        Thread.sleep(1000);
        //GPSService dataService = (GPSService) SpringContextUtil.getBean("GPSService");

        return new Response("消息" + message.getName() + "已下发!");
    }

   /**
    *
    * http://localhost:9001/ws
    */

    @RequestMapping(value = "/send", method = RequestMethod.POST)
    @ResponseBody
    public void say2(@RequestBody String requestBody, HttpServletRequest request)throws Exception
    {
        JSONObject jsonObject = JSON.parseObject(requestBody);
 ws.sendMessage(jsonObject.getString("data"));
    }
}
